package com.rivulet.doj_backend.judger.judging;
import com.rivulet.doj_backend.pojo.JudgeCode;
import com.rivulet.doj_backend.pojo.JudgeParameter;

import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * {一句话形容此class功能}
 *
 * @author xujiac
 * @create 2021/4/27
 * @since 1.0.0
 */
public class JudgeNumTwo {
    private static String fileName="D:\\doj_workspace";
    private JudgeParameter parameter;
    public JudgeNumTwo(JudgeParameter parameter){
        this.parameter=parameter;
    }
    public void judge(){
        checkFileName(parameter.getUser_id().toString());
        creatFile();
        creatCodeFile(parameter.getCode(),parameter.getCode_type());
        String[] command= chooseCommand(parameter.getCode_type());
        String [] parameters=parameter.getCode_parameter();
        String [] results=new String[5];
        Integer [] running_time=new Integer [5];
        try {
            for(int i=0;i<5;i++) {
                Date beginTime=new Date();
                Process process=Runtime.getRuntime().exec(command,null,new File(fileName));
                BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
                writer.write(parameters[i]);
                writer.close();
                SequenceInputStream allInputStream=new SequenceInputStream(process.getInputStream(),process.getErrorStream());
                InputStreamReader reader=new InputStreamReader(allInputStream,"utf-8");
                BufferedReader bufferedReader=new BufferedReader(reader);
                String res="";
                StringBuilder str=new StringBuilder();
                while ((res=bufferedReader.readLine())!=null){
                    str.append(res+"\n");
                }
                bufferedReader.close();
                process.waitFor();
                process.destroy();
                Date endTime=new Date();
                long time=endTime.getTime()-beginTime.getTime();
                results[i]=str.toString();
                running_time[i]=(int) time;
            }
            parameter.setCode_result(results);
            parameter.setRunning_time(running_time);
        }catch (Exception e){
            System.out.println(e);
        }finally {
            deleteDir(new File(fileName));
        }
    }

    private String[] chooseCommand(String languageType){
        if(languageType.equals("java")){
            return new String[]{ "cmd","/c","java Main.java"};
        }else if(languageType.equals("cpp")){
            creatBatFile();
            return new String[]{ "cmd","/c","cpp.bat"};
        }else if(languageType.equals("py")){
            return new String[]{ "cmd","/c","Python Main.py"};
        }else {
            return null;
        }
    }

    private void creatCodeFile(String code,String languageType){
        File file=new File(fileName+File.separator+"Main."+languageType);
        try {
            if(!file.exists()){
                file.createNewFile();
                BufferedWriter writer=new BufferedWriter(new FileWriter(file));
                writer.write(code);
                writer.close();
            }
        }catch (Exception e){
            System.out.println(e);
        }
    }

    private void creatBatFile(){
        File file=new File(fileName+File.separator+"cpp.bat");
        try {
            if(file.exists()){
                BufferedWriter writer=new BufferedWriter(new FileWriter(file));
                String cppBat="@echo off\n" +
                        "g++ -o main Main.cpp\n" +
                        "Main.exe";
                System.out.println(cppBat);
                writer.write(cppBat);
                writer.close();
            }else {
                file.createNewFile();
                BufferedWriter writer=new BufferedWriter(new FileWriter(file));
                String cppBat="@echo off\n" +
                        "g++ -o main Main.cpp\n" +
                        "Main.exe";
                System.out.println(cppBat);
                writer.write(cppBat);
                writer.close();
            }
        }catch (IOException e){
            System.out.println(e);
        }
    }

    private void creatFile(){
        File file=new File(fileName);
        if(file.exists()){
            return;
        }else {
            file.mkdir();
        }
    }

    private void checkFileName(String user_id){
        fileName=fileName+File.separator+user_id;
    }

    private static void deleteDir(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                deleteDir(files[i]);
            }
        }
        file.delete();
    }
}
